/*
 * TraceDeclarator.c
 *
 *  Created on: 2012/06/07
 *      Author: guanxiaoying
 */
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include "../tracedef.h"
#include "TraceAst.h"
S4 TraceDeclarator( VOID ){
	S4 iRet = TRACEEXIT_SUCCESS;
	AST astMaxKind;
	AST astTraceStackUpon;
	U2 usRet;
	/*debug,12/11/14,S*/
	AST astTmp = 0;
	/*debug,12/11/14,E*/

	if(lEventSec & EVT_DECLARATOR )
	{
		iRet = pfunc( stCurrTraceInfo.usKind, stCurrTraceInfo.astTable , lEvent | EVT_DECLARATOR );
		if(iRet> TRACEEXIT_WARNING)
		{
			return(iRet);
		}
	}
	astMaxKind = stTraceStackAccess.getMax();
	usRet = stTraceStackAccess.kind.get(astMaxKind);
	astTraceStackUpon = stTraceStackAccess.currObject.get(astMaxKind);
	if( astTraceStackUpon != stCurrTraceInfo.astTable
			|| usRet !=  stCurrTraceInfo.usKind ){
		 /*式文をSTACKへPUSHする*/
		stCurrTraceInfo.astNext = CDeclarator.getNextDeclarator( stCurrTraceInfo.astTable );
		iRet = TraceStack( STACK_PUSH );
		if( iRet > TRACEEXIT_WARNING )
		{
			return iRet;
		}
		astMaxKind = stTraceStackAccess.getMax();
		/* 式文のトレース回数を1とする */
		stTraceStackAccess.times.set( astMaxKind, 1 );
		/*カレントトレース対象を式文の式とする*/
		stCurrTraceInfo.astTable =  CDeclarator.getChildDeclarator( stCurrTraceInfo.astTable  );
		stCurrTraceInfo.astNext = 0 ;
	}else{
		usRet = stTraceStackAccess.times.get( astMaxKind );
		if( usRet == 1 ){
			if( CDeclarator.isInitialExpDeclarator( stCurrTraceInfo.astTable ) ){
				stCurrTraceInfo.usKind = TR_KIND_INITIALIZER;
				stCurrTraceInfo.astTable =  CDeclarator.initExp_getInitExp( stCurrTraceInfo.astTable  );
				stCurrTraceInfo.astNext = 0 ;
				/*debug,12/11/14,S*/
				stTraceStackAccess.times.set( astMaxKind, usRet + 1 );
				/*debug,12/11/14,E*/
			}else if( CDeclarator.isArrayDeclarator( stCurrTraceInfo.astTable ) ){
				stCurrTraceInfo.usKind = TR_KIND_EXPRESSION;
				stCurrTraceInfo.astTable =  CDeclarator.array_getArrayIndex( stCurrTraceInfo.astTable  );
				stCurrTraceInfo.astNext = 0 ;
				/*debug,12/11/14,S*/
				stTraceStackAccess.times.set( astMaxKind, usRet + 1 );
				/*debug,12/11/14,E*/
			/*debug,12/11/14,S*/
			}else if(CDeclarator.isSymbolDeclarator(stCurrTraceInfo.astTable)){
				stTraceStackAccess.times.set( astMaxKind, usRet + 1 );
				astTmp = CDeclarator.symbol_getSymbol(stCurrTraceInfo.astTable);
				if(CSymbol.isFunction(astTmp)){
					astTmp = CSymbol.getFunctionBody(astTmp);
					if(astTmp > 0){
						stCurrTraceInfo.usKind = TR_KIND_STATEMENT;
						stCurrTraceInfo.astTable =  astTmp;
						stCurrTraceInfo.astNext = CStatement.getNextStmt(astTmp) ;
					}
				}
			}
			/*debug,12/11/14,E*/
			else{
				/*式文のトレース回数をトレース最大回数とする */
				astMaxKind = stTraceStackAccess.getMax();
				stTraceStackAccess.times.set( astMaxKind, TRACE_OBJECT_END );

				/* カレントトレース対象を空とする */
				stCurrTraceInfo.astNext = 0;
				stCurrTraceInfo.astTable = 0;
			}
		}else{
			/*式文のトレース回数をトレース最大回数とする */
			astMaxKind = stTraceStackAccess.getMax();
			stTraceStackAccess.times.set( astMaxKind, TRACE_OBJECT_END );

			/* カレントトレース対象を空とする */
			stCurrTraceInfo.astNext = 0;
			stCurrTraceInfo.astTable = 0;
		}
	}
	return iRet;
}
